﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Corina.Datamodel.Entities;
using System.Linq;
using Corina.Commands;

namespace Corina.Cydee.Apphub.Viewmodels
{
    public class SpecimenKindViewmodel : ViewmodelBase
    {
        public SpecimenKindViewmodel()
        {
            DeviceClient.GetSpecimenKindItemsCompleted += GetSpecimenKindItemsCompleted;
            DeviceClient.InsertSpecimenKindCompleted += InsertSpecimenKindCompleted;
            DeviceClient.UpdateSpecimenKindCompleted += UpdateSpecimenKindCompleted;
            DeviceClient.RemoveSpecimenKindCompleted += RemoveSpecimenKindCompleted;

            if (DataContext.IsValid)
            {
                LoadData();
            }
        }

        #region Methods
        private void RemoveSpecimenKindCompleted(object sender, DataService.RemoveSpecimenKindCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                LoadData();
            }
        }

        private void UpdateSpecimenKindCompleted(object sender, DataService.UpdateSpecimenKindCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                Title = string.Empty;
                SpecimenKindId = 0;

                LoadData();
            }
        }

        private void InsertSpecimenKindCompleted(object sender, DataService.InsertSpecimenKindCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                LoadData();

                Title = string.Empty;
            }
        }

        private void GetSpecimenKindItemsCompleted(object sender, DataService.GetSpecimenKindItemsCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                ItemsCollection = new ObservableCollection<SpecimenKindInfo>(e.Result.Entities.OfType<SpecimenKindInfo>());
                IsBusy = false;
            }
        }

        protected override void LoadData(params object[] paras)
        {
            IsBusy = true;
            DeviceClient.GetSpecimenKindItemsAsync();
        }
        #endregion

        #region Collection
        private ObservableCollection<SpecimenKindInfo> _itemsCollection;
        public ObservableCollection<SpecimenKindInfo> ItemsCollection
        {
            get { return _itemsCollection; }
            set {
                _itemsCollection = value;
                RaisePropertyChanged(() => ItemsCollection);
            }
        }
        #endregion

        #region Update
        private string _title = string.Empty;
        public string Title
        {
            get { return _title; }
            set {
                _title = value;
                RaisePropertyChanged(() => Title);
                RaisePropertyChanged(() => SubmitCommand);
            }
        }

        private int _specimenKindId = 0;
        public int SpecimenKindId
        {
            get { return _specimenKindId; }
            private set
            {
                _specimenKindId = value;
                RaisePropertyChanged(() => SpecimenKindId);
            }
        }

        #endregion

        #region Command
        public ICommand SubmitCommand
        {
            get {
                return new GenericCommand()
                {
                    CanExecuteCallback = delegate { return !string.IsNullOrEmpty(Title); },
                    ExecuteCallback = delegate
                    {
                        if (SpecimenKindId > 0)
                        {
                            DeviceClient.UpdateSpecimenKindAsync(Account, Password, Title, SpecimenKindId);
                        }
                        else
                        {
                            DeviceClient.InsertSpecimenKindAsync(Account, Password, Title);
                        }
                    }
                };
            }
        }

        public ICommand UpdateCommand
        {
            get
            {
                return new DelegateCommand(t =>
                {
                    SpecimenKindInfo entity = t as SpecimenKindInfo;
                    Title = entity.Title;
                    SpecimenKindId = entity.Id;
                });
            }
        }

        public ICommand RemoveCommand
        {
            get {
                return new DelegateCommand(t =>
                {
                    int id = Convert.ToInt32(t);
                    if (ShowConfirm(Language.LanguageKeys.IDS_DELETE_CONFIRM) == MessageBoxResult.Yes)
                    {
                        DeviceClient.RemoveSpecimenKindAsync(Account, Password, id);
                    }
                });
            }
        }

        public ICommand CancelCommand
        {
            get {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate
                    {
                        Title = string.Empty;
                        SpecimenKindId = 0;
                    }
                };
            }
        }
        #endregion
    }
}
